//
//  ForSalePaperViewController.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/3/28.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "ForSalePaperViewController.h"
#import "AllMovieCommentViewController.h"
#import "SureGroupIndentViewController.h"
#import "SharePopView.h"
@interface ForSalePaperViewController ()<DYQBannerScrollViewDelegate>
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UIScrollView *scroView;
@property (strong, nonatomic) IBOutlet UIView *payView;
@property (strong,nonatomic) DYQBannerScrollView *bannerScrollView;//广告栏
@property (nonatomic,strong)  NSMutableArray  *imageArray;

@property (strong, nonatomic) IBOutlet UILabel *functionTime;

@property (strong, nonatomic) IBOutlet UILabel *usefulTime;

@property (strong, nonatomic) IBOutlet UILabel *shopName;
@property (strong, nonatomic) IBOutlet UILabel *shopInfo;
@property (strong, nonatomic) IBOutlet UILabel *shopPrice;
@property (strong, nonatomic) IBOutlet UILabel *shopOldPrice;
@property (strong, nonatomic) IBOutlet UILabel *shopSoldNum;
@property (strong, nonatomic) IBOutlet UILabel *shopScore;
@property (strong, nonatomic) IBOutlet UILabel *shopCommentNumber;
@property (strong, nonatomic) IBOutlet UILabel *shopSecondName;
@property (strong, nonatomic) IBOutlet UILabel *shopAddress;
@property (strong, nonatomic) IBOutlet UILabel *shopPhone;

@property(nonatomic,strong)NSDictionary * shopInfoDic;
@end

@implementation ForSalePaperViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _imageArray = [NSMutableArray array];

    // Do any additional setup after loading the view.
    [self initViewLayout];
//    [self getShoppingDetail];
//    [self haveToKnow];
//    [self getServiceInfo];
//    [self getAdvertiseBanner];
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroView];
    [self adaptiveViewLayout:self.payView];
    _scroView.contentSize = flexibleSize(CGSizeMake(0, 540), NO);
    
    CommitStarsView * CommentStars = [[CommitStarsView alloc]initWithFrame:flexibleFrame(CGRectMake(14, 243, 80, 15), NO) Stars:3.5];
    [_scroView addSubview:CommentStars];
    
    //广告栏
    self.bannerScrollView = [[DYQBannerScrollView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 0, 320, 150), NO)];
    self.bannerScrollView.backgroundColor = [UIColor clearColor];
    self.bannerScrollView.pageControlPosition = PageControlPositionCenter;
    self.bannerScrollView.noDataPlaceholderImage = [UIImage imageNamed:@"cover_cy"];
    self.bannerScrollView.timeInterval = 3;
    self.bannerScrollView.pageControllerTintColor = [UIColor whiteColor];
    self.bannerScrollView.pageControllerCurrentColor = [UIColor orangeColor];
    
    [self.scroView insertSubview:_bannerScrollView belowSubview:_shopName];
}

- (IBAction)back:(id)sender {
    [self.navigationController  popViewControllerAnimated:YES];
}

#pragma mark  获取首页广告BANNER--=========================================
-(void)getAdvertiseBanner{
    
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:_groupShoppingId forKey:@"projectId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_queryAppGroupImages withHttpType:@"POST" withTag:SX_SX_queryAppGroupImages];
}


#pragma mark == 查询团购详情
- (void)getShoppingDetail{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:_groupShoppingId forKey:@"mgpdId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_queryGrouponStoreInfo withHttpType:@"POST" withTag:SX_SX_queryGrouponStoreInfo];
}
#pragma mark == 查询购买须知
- (void)haveToKnow{
    
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:_groupShoppingId forKey:@"mgpdId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_queryAppBuyNotes withHttpType:@"POST" withTag:SX_SX_queryAppBuyNotes];
}
#pragma mark == 查询是否随时退  免预约
- (void)getServiceInfo{
    
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:_groupShoppingId forKey:@"projectId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_queryAppGroupBuyingRules withHttpType:@"POST" withTag:SX_SX_queryAppGroupBuyingRules];
}
#pragma mark == 分享
- (IBAction)shareHandel:(id)sender {
    SharePopView * popView = [[SharePopView alloc]initWithTitles:@[@"微信好友",@"朋友圈",@"新浪微博",@"QQ好友"] complect:^(NSInteger index) {
        switch (index) {
            case 0:
            {
                [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatSession] content:@"" image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
                    if (response.responseCode == UMSResponseCodeSuccess) {
                        //            NSLog(@"分享成功！");
                    }
                }];
                
            }
                break;
            case 1:
            {
                [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatTimeline] content:@"" image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
                    if (response.responseCode == UMSResponseCodeSuccess) {
                        //            NSLog(@"分享成功！");
                    }
                }];
            }
                break;
            case 2:
            {
                [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToSina] content:@"" image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
                    if (response.responseCode == UMSResponseCodeSuccess) {
                        //            NSLog(@"分享成功！");
                    }
                }];
            }
                break;
            case 3:
            {
                [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToQQ] content:@"" image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
                    if (response.responseCode == UMSResponseCodeSuccess) {
                        //            NSLog(@"分享成功！");
                    }
                }];
            }
                break;
            default:
                break;
        }

    }];
    [popView show];
}


#pragma mark == 评论
- (IBAction)commentshandel:(id)sender {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
    AllMovieCommentViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"AllMovieComment"];
    page.type = 2;
    page.commentId = _groupShoppingId;
    [self.navigationController   pushViewController:page animated:YES];
}
#pragma mark  == 拨打电话
- (IBAction)callStorePhone:(UIButton *)sender {
    [Common callPhone:_shopPhone.text view:sender];
}
#pragma mark == 立即购买
- (IBAction)buyNow:(id)sender {
    
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
    SureGroupIndentViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"SureGroupIndent"];
    page.groupShoppingId = _groupShoppingId;
    page.shopInfoDic = _shopInfoDic;
    [self.navigationController   pushViewController:page animated:YES];
    
}

#pragma mark----DYQBannerScrollViewDelegate

-(void)bannerScrollView:(DYQBannerScrollView *)bannerScrollView didSelectItemAtIndex:(NSInteger)index
{
    //    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
    //    WebDetailInfoViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"WebDetailInfo"];
    //    page.objectId = _bannerArray[index][@"appAdertUrl"];
    //    page.titleStr = @"详情";
    //    page.meType = @"4";
    //    [self.navigationController  pushViewController:page animated:YES];
}

#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    if (tag == SX_SX_queryGrouponStoreInfo){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            
            NSDictionary * dic = database[@"result"];
            _shopInfoDic = dic;
            //        NSString * image = dic[@"storeImg"];
            //        if (image) {
            //            [cell.shoppingImage sd_setImageWithURL:[NSURL URLWithString:image]];
            //        }else{
            //            cell.shoppingImage.image = [UIImage imageNamed:@"gsInfo1"];
            //        }
            _shopName.text = dic[@"storeName"];
            _shopInfo.text = [NSString stringWithFormat:@"%@人餐，节假日通用！",dic[@"mgmnNumber"]];
            _shopPrice.text = [NSString stringWithFormat:@"¥%.2f",[dic[@"mgpiGourponAmount"] floatValue]];
            _shopOldPrice.text = [NSString stringWithFormat:@"门市价：¥%@",dic[@"mgpiOrginalAmount"]];
            _shopSoldNum.text = [NSString stringWithFormat:@"已售%@",dic[@"mgprSelledCount"]];
            _shopScore.text = [NSString stringWithFormat:@"%.1f分",[dic[@"mgprComm"] floatValue]/2];
            _shopCommentNumber.text = [NSString stringWithFormat:@"%@人评论",dic[@"mgprNum"]];
            _shopSecondName.text = dic[@"storeName"];
            _shopAddress.text = dic[@"storeAddress"];
            _shopPhone.text = dic[@"storePhone"];
            //mgprComm
            CommitStarsView * CommentStars = [[CommitStarsView alloc]initWithFrame:flexibleFrame(CGRectMake(14, 243, 80, 15), NO) Stars:[dic[@"mgprComm"] floatValue]/2];
            [_scroView addSubview:CommentStars];
        }
    }else if (tag == SX_SX_queryAppBuyNotes){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            NSDictionary * dic = database[@"result"];
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateStyle:NSDateFormatterMediumStyle];
            [formatter setTimeStyle:NSDateFormatterShortStyle];
            [formatter setDateFormat:@"yyyy.MM.dd"];
            if(![dic[@"mgprAddDate"] isEqual:[NSNull null]]){
                NSDate *start = [NSDate dateWithTimeIntervalSince1970:[dic[@"mgprAddDate"] integerValue]/1000];
                if(![dic[@"mgvaValidityDate"] isEqual:[NSNull null]]){
                    NSDate *end = [NSDate dateWithTimeIntervalSince1970:[dic[@"mgvaValidityDate"] integerValue]/1000];
                    _functionTime.text = [NSString stringWithFormat:@"%@-%@",[formatter stringFromDate:start],[formatter stringFromDate:end]];
                }
                [formatter setDateFormat:@"HH"];
                NSDate *startH = [NSDate dateWithTimeIntervalSince1970:[dic[@"mguaBeginHour"] integerValue]/1000];
                NSDate *endH = [NSDate dateWithTimeIntervalSince1970:[dic[@"mguaEndHour"] integerValue]/1000];
                NSString * startHour = [formatter stringFromDate:startH];
                NSString * endHour = [formatter stringFromDate:endH];
                
                [formatter setDateFormat:@"MM"];
                NSDate *startM = [NSDate dateWithTimeIntervalSince1970:[dic[@"mguaBeginMinute"] integerValue]/1000];
                NSDate *endM = [NSDate dateWithTimeIntervalSince1970:[dic[@"mguaEndMinute"] integerValue]/1000];
                NSString * startMin = [formatter stringFromDate:startM];
                NSString * endMin = [formatter stringFromDate:endM];
                
                _usefulTime.text = [NSString stringWithFormat:@"%@:%@-%@:%@",startHour,startMin,endHour,endMin];
            }else{
                
            }
//            _userKnowRule.text = dic[@"mgaiContent"];
            
            //            NSLog(@"%@",dic[@"mgaiContent"]);
        }
        
    }else if (tag == SX_SX_queryAppGroupBuyingRules){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            NSMutableArray * nameArray = [NSMutableArray array];
            NSDictionary * dic = database[@"result"];
            
            if ([[NSString stringWithFormat:@"%@",dic[@"mgreIsDueRefund"]] isEqualToString:@"1"]) {
                [nameArray addObject:@" 随时退"];
            }
            
            
            if ([[NSString stringWithFormat:@"%@",dic[@"mgreIsAnytimeRefund"]] isEqualToString:@"1"]) {
                
                [nameArray addObject:@" 过期退"];
            }
            
            
            if ([[NSString stringWithFormat:@"%@",dic[@"mgsnReservationType"]] isEqualToString:@"1"]) {
                
                [nameArray addObject:@" 免预约"];
            }else{
                [nameArray addObject:@" 需预约"];
            }
            
            for (int i = 0; i<nameArray.count; i++) {
                UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame = flexibleFrame(CGRectMake(15+70*i, 202, 65, 22), NO);
                [button setTitle:nameArray[i] forState:UIControlStateNormal];
                [button setTitleColor:[UIColor colorWithRed:0.569 green:0.757 blue:0.153 alpha:1.000] forState:UIControlStateNormal];
                [button setImage:[UIImage imageNamed:@"groupShopDetail1"] forState:UIControlStateNormal];
                button.titleLabel.font = [UIFont systemFontOfSize:12*VerticalTexeRatio()];
                [_scroView addSubview:button];
            }
        }
        
    }else if (tag == SX_SX_queryAppGroupImages){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [self.imageArray removeAllObjects];
            self.imageArray = database[@"result"];
            for (NSDictionary * dic in _imageArray) {
                NSString * picture = [Common stringFromData:dic[@"mgriImageUrl"]];
                [_imageArray addObject:picture];
            }
        _bannerScrollView.imageUrls = _imageArray;
        _bannerScrollView.delegate = self;
        
        }
    }
}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    //
    [self dismissWaitDialog];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
